using System;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace NewsReaderApp.Views
{
    public class ExtendedSplash : Grid
    {
        private Rect splashImageRect; // Rect to store splash screen image coordinates.
        private bool dismissed; // Variable to track splash screen dismissal status.
        private Frame rootFrame;
        SplashScreen splash;


        public ExtendedSplash(Windows.ApplicationModel.Activation.SplashScreen splashscreen, bool loadState)
        {
            InitializeComponent();
            // Listen for window resize events to reposition the extended splash screen image accordingly.
            // This is important to ensure that the extended splash screen is formatted properly in response to snapping, unsnapping, rotation, etc...
            Window.Current.SizeChanged += ExtendedSplash_OnResize;

            if (splash != null)
            {
                // Register an event handler to be executed when the splash screen has been dismissed.
                splash.Dismissed += DismissedEventHandler;

                // Retrieve the window coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }

            // Create a Frame to act as the navigation context 
            rootFrame = new Frame();
            SuspensionManager.RegisterFrame(rootFrame, "appFrame");

            // Normally you should start the time consuming task asynchronously here
            // and dissmiss the extended splash screen in the completion handler for that task
            // In this sample we dismiss the extended splash screen in the handler for 
            // "Learn More" button for demonstration.

            // Here Restore the saved session state if necessary
            if (loadState)
                SuspensionManager.RestoreAsync();
        }

        // Position the extended splash screen image in the same location as the system splash screen image.
        void PositionImage()
        {
            Canvas.SetTop(extendedSplashImage, splashImageRect.Y);
            Canvas.SetLeft(extendedSplashImage, splashImageRect.X);
            extendedSplashImage.Height = splashImageRect.Height;
            extendedSplashImage.Width = splashImageRect.Width;
        }

        void ExtendedSplash_OnResize(Object sender, WindowSizeChangedEventArgs e)
        {
            // Safely update the extended splash screen image coordinates. This function will be fired in response to snapping, unsnapping, rotation, etc...
            if (splash != null)
            {
                // Update the coordinates of the splash screen image.
                splashImageRect = splash.ImageLocation;
                PositionImage();
            }
        }

        void LearnMoreButton_Click(Object sender, RoutedEventArgs e)
        {
            if (rootFrame.Content == null)
            {
                // Navigate to mainpage    
                if (!rootFrame.Navigate(TypeName(MainPage.typeid)))
                {
                    throw new Exception("Failed to create main page");
                }
            }
            // Set extended splash parameters on the main page
            ((MainPage)rootFrame.Content).SetExtendedSplashInfo(splashImageRect, dismissed);

            // Place the frame in the current Window 
            Window.Current.Content = rootFrame;
        }

        // Include code to be executed when the system has transitioned from the splash screen to the extended splash screen (application's first view).
        void DismissedEventHandler(Windows.ApplicationModel.Activation.SplashScreen sender, Object e)
        {
            dismissed = true;

            // Navigate away from the app's extended splash screen after completing setup operations here...
            // This sample navigates away from the extended splash screen when the "Learn More" button is clicked.
        }

    }
}